package com.example.eduservice.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.EduCourse;
import com.example.eduservice.entity.EduCourseDescription;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.entity.vo.CourseInfoVo;
import com.example.eduservice.entity.vo.CoursePublishVo;
import com.example.eduservice.mapper.EduCourseMapper;
import com.example.eduservice.service.EduChapterService;
import com.example.eduservice.service.EduCourseDescriptionService;
import com.example.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eduservice.service.EduVideoService;
import com.example.servicebase.exceptionHandler.EduException;
import org.ehcache.config.ResourceType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.nio.charset.CoderResult;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Anticipate
 * @since 2022-03-13
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    // 课程描述的注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    // 课程小节注入
    @Autowired
    private EduVideoService videoService;

    // 课程章节注入
    @Autowired
    private EduChapterService chapterService;

    // 添加课程基本信息
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        // 1.向课程表添加课程基本信息
        // CourseInfo对象转换为eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0) {
            // 添加失败
            throw new EduException(20001, "添加课程信息失败");
        }

        // 获取课程信息表的id
        String cid = eduCourse.getId();

        // 2.向课程简介表添加课程简介
        // edu_course_description
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(cid);
        boolean save = courseDescriptionService.save(eduCourseDescription);
        if (!save) {
            throw new EduException(20001, "课程简介信息保存失败");
        }

        return cid;
    }

    // 根据课程id查询信息
    @Override
    public CourseInfoVo getCourseById(String courseId) {
        // 1. 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        // 2. 查询描述表
        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }


    // 修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        // 1. 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update == 0) {
            throw new EduException(20001, "修改课程信息失败！");
        }


        // 2. 修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo.getDescription(), eduCourseDescription);
        courseDescriptionService.updateById(eduCourseDescription);
        // if (!descriptionUpdate) {
        //     throw new EduException(20001, "修改课程信息失败！");
        // }


    }

    // 查询发布课程信息
    @Override
    public CoursePublishVo selectCoursePublishVoById(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.selectCoursePublishVoById(courseId);
        return coursePublishVo;
    }


    /*
    * 根据课程id删除课程
    * 首先要删除课程下面的章节、小节和描述
    * 这里不能使用例如：chapterService.removeById(courseId);
    * 因为传入的参数是courseId 但是直接调用removeById 使用的不是courseId
    * 所以只能自定义方法，写入service
    * */
    @Override
    public void deleteCourseByCourseId(String courseId) {
        // 1.根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);
        // 2.根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);
        // 3.根据课程id删除描述
        courseDescriptionService.removeDescriptionByCourseId(courseId);
        // 4.根据课程id删除课程本身
        baseMapper.deleteById(courseId);
    }
}
